#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
    int subarraysDivByK(vector<int>& nums, int k) {
        int sum = 0, ret = 0;
        unordered_map<int, int> hash;
        ++hash[0];
        for (int i = 0; i < nums.size(); ++i)
        {
            sum += nums[i];
            int n = (sum % k + k) % k;
            if (hash.count(n))
                ret += hash[n];
            hash[n]++;
        }
        return ret;
    }
};